package com.baomidou.springwind.excel;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.entity.DeliveryGoods;
import com.baomidou.springwind.entity.NetworkNode;
import com.baomidou.springwind.enums.NodeTypeEnum;
import com.baomidou.springwind.service.DeliveryGoodsService;
import com.baomidou.springwind.service.NetworkNodeService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author luox
 */
@Component
public class NetworkDataReader extends ExcelReader<NetworkNode> {

    @Autowired
    private NetworkNodeService networkNodeService;

    @Override
    protected String saveData(List<NetworkNode> dataList, Long batchId) {
        EntityWrapper<NetworkNode> wrapper = new EntityWrapper<NetworkNode>();
        wrapper.eq("batch_id", batchId);
        deleteFile(batchId);
        networkNodeService.delete(wrapper);//删除之前导入的数据
        for (NetworkNode node : dataList) {
            node.setBatchId(batchId);
            node.setOperatorId(getCurrentUserId());
            node.setCreateTime(new Date());
        }
        networkNodeService.insertBatch(dataList, 100);
        return "";
    }

    private void deleteFile(Long batchId) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String path = request.getSession().getServletContext().getRealPath("");
        path = path.substring(0, path.lastIndexOf("\\"));
        path = path.substring(0, path.lastIndexOf("\\"));
        File file = new File(path + "/temp/route/" + batchId + ".obj");
        file.delete();
    }

    @Override
    protected List<NetworkNode> verifyData(HSSFWorkbook workbook) {
        List<NetworkNode> nodeList = new ArrayList<NetworkNode>();
//        nodeList.addAll(buildFactoryNode(workbook));
        nodeList.addAll(buildDistributeCenterNode(workbook));
        nodeList.addAll(buildCustomNode(workbook));
        return nodeList;
    }

    @Override
    protected List<NetworkNode> verifyData7(XSSFWorkbook workbook) {
        return null;
    }


    private List<NetworkNode> buildFactoryNode(HSSFWorkbook workbook) {
        List<NetworkNode> nodeList = new ArrayList<NetworkNode>();
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        NetworkNode node;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            node = new NetworkNode();
            node.setNodeType(NodeTypeEnum.FACTORY.type);
            node.setNodeName(getStringCellValue(sheet.getRow(i), 0));
            node.setCoordinateX(new BigDecimal(getStringCellValue(sheet.getRow(i), 1)));
            node.setCoordinateY(new BigDecimal(getStringCellValue(sheet.getRow(i), 2)));
            nodeList.add(node);
        }
        return nodeList;
    }

    private List<NetworkNode> buildDistributeCenterNode(HSSFWorkbook workbook) {
        List<NetworkNode> nodeList = new ArrayList<NetworkNode>();
        HSSFSheet sheet = workbook.getSheetAt(1);
        int lastRowNum = sheet.getLastRowNum();
        NetworkNode node;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            node = new NetworkNode();
            node.setNodeType(NodeTypeEnum.DISTRIBUTE_CENTER.type);
            node.setNodeName(getStringCellValue(sheet.getRow(i), 0));
            node.setCoordinateX(new BigDecimal(getStringCellValue(sheet.getRow(i), 1)));
            node.setCoordinateY(new BigDecimal(getStringCellValue(sheet.getRow(i), 2)));
            nodeList.add(node);
        }
        return nodeList;
    }

    private List<NetworkNode> buildCustomNode(HSSFWorkbook workbook) {
        List<NetworkNode> nodeList = new ArrayList<NetworkNode>();
        HSSFSheet sheet = workbook.getSheetAt(2);
        int lastRowNum = sheet.getLastRowNum();
        NetworkNode node;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            node = new NetworkNode();
            node.setNodeType(NodeTypeEnum.CUSTOM.type);
            node.setNodeName(getStringCellValue(sheet.getRow(i), 0));
            node.setCoordinateX(new BigDecimal(getStringCellValue(sheet.getRow(i), 1)));
            node.setCoordinateY(new BigDecimal(getStringCellValue(sheet.getRow(i), 2)));
            node.setAmount(new BigDecimal(getStringCellValue(sheet.getRow(i), 3)));
            nodeList.add(node);
        }
        return nodeList;
    }
}
